# Maintainer: Felix Yan <felixonmars@archlinux.org>
# Contributor: Arch Haskell Team <arch-haskell@haskell.org>

_hkgname=postgresql-binary
pkgname=haskell-postgresql-binary
pkgver=0.12.5
pkgrel=71
pkgdesc="Encoders and decoders for the PostgreSQL's binary format"
url="https://github.com/nikita-volkov/postgresql-binary"
license=("MIT")
arch=('loong64' 'x86_64')
depends=('ghc-libs' 'haskell-aeson' 'haskell-binary-parser' 'haskell-bytestring-strict-builder'
         'haskell-network-ip' 'haskell-scientific' 'haskell-unordered-containers'
         'haskell-uuid' 'haskell-vector')
makedepends=('ghc' 'uusi' 'haskell-postgresql-libpq' 'haskell-tasty' 'haskell-tasty-quickcheck'
             'haskell-tasty-hunit' 'haskell-quickcheck' 'haskell-quickcheck-instances'
             'haskell-rerebase' 'pifpaf' 'postgresql')
source=("https://hackage.haskell.org/packages/archive/${_hkgname}/${pkgver}/${_hkgname}-${pkgver}.tar.gz")
sha512sums=('471bd8c9fb97ef891fde094f616f246d6228f8eccc1c17a711e84604b1ea1f157f37ada4cfeb670d71afc83df11158cc16227d254bd8c00024dbf4351b6ac7ad')

prepare() {
    cd $_hkgname-$pkgver
    gen-setup
    # Do not use default postgres port
    sed -i 's/5432/9824/' tasty/Main/DB.hs
}

build() {
    cd $_hkgname-$pkgver

    runhaskell Setup configure -O --enable-shared --enable-executable-dynamic --disable-library-vanilla \
        --prefix=/usr --docdir=/usr/share/doc/$pkgname --datasubdir=$pkgname --enable-tests \
        --dynlibdir=/usr/lib --libsubdir=\$compiler/site-local/\$pkgid --ghc-option=-fllvm
    runhaskell Setup build $MAKEFLAGS
    runhaskell Setup register --gen-script
    runhaskell Setup unregister --gen-script
    sed -i -r -e "s|ghc-pkg.*update[^ ]* |&'--force' |" register.sh
    sed -i -r -e "s|ghc-pkg.*unregister[^ ]* |&'--force' |" unregister.sh
}

check() {
    cd $_hkgname-$pkgver

    eval $(pifpaf run postgresql --host 127.0.0.1)
    createuser -s postgres
    runhaskell Setup test --show-details=direct
    pifpaf_stop
}

package() {
    cd $_hkgname-$pkgver

    install -D -m744 register.sh "$pkgdir"/usr/share/haskell/register/$pkgname.sh
    install -D -m744 unregister.sh "$pkgdir"/usr/share/haskell/unregister/$pkgname.sh
    runhaskell Setup copy --destdir="$pkgdir"
    install -D -m644 LICENSE "$pkgdir"/usr/share/licenses/$pkgname/LICENSE
    rm -f "$pkgdir"/usr/share/doc/$pkgname/LICENSE
}
